<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ShopController extends Controller
{
    protected $api_url = [
        'index' => ['get', 'shop'],
        'update' => ['PUT', 'shop/enable/%d'],
        'recommend'  => ['put', 'shop/recommend/%d'],
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_backend');
    }

    public function indexAction()
    {
//        $this->validate($this->request, [
//            'page' => 'integer',
//            'sp_name' => 'string',
//        ]);

        $search_params = $this->request->all(['sp_name','sp_created','sp_status']);

        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy' => 'sp_id',
            'sortedBy' => 'desc',
            //'with'=>'shopLevel;company;shopBanners;product;companyMaterial',
            'with'=>'shopLevel;company;shopBanners;companyMaterial',
            'page' => $page
        ];
        $ret = http_admin($this->api_url['index'])->send($params);

        $data = [];
        if (isset($ret['code']) && $ret['code'] === 0 && !empty($ret['data'])) {
            $data = $ret['data'];
        }
        $data['search_params'] = $search_params;

        return view('Admin.shop.index', paginate($this->request, $data));
    }

    public function addAction()
    {
        print_r('系统后台店铺添加！');
    }

    public function editAction()
    {
        dd('系统后台店铺编辑！');
    }
    // 后台编辑保存
    public function updateAction()
    {
        $params = $this->request->all(['status','sp_id','description']);
        $send=[
            'sp_status' => $params['status'],
            'description' => $params['description'],
        ];
        $ret = http_admin($this->api_url['update'],[$params['sp_id']])->send($send);
        if ($ret['code']==0){
            return response()->json(['code'=>0,'message'=>'修改成功','params'=>$ret]);
        }else{
            return response()->json(['code'=>1,'message'=>'修改失败']);
        }
    }

    public function deleteAction()
    {
        print_r('系统后台店铺删除！');
    }

    /**
     * 推荐
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommend($id){
        //请求部分
        $data=$this->request->all(['is_recommend']);
        $ret = http_admin($this->api_url['recommend'],[$id])->send($data);
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);
    }
}